﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ClientLib;

namespace TinyCClient
{
    public partial class MainForm : Form, ClientLib.ClientFW
    {
        public
        TreeNode m_SelectedNode;
        TinyMsgToolLib.TinyMsgInterface TCPclient = new TinyMsgToolLib.TinyMsgInterface();
        ClientLib.TinyClientRuntime m_runtime;
        Dictionary<string, UserInfo> m_UserInfoFormMgr = new Dictionary<string, UserInfo>();
        Dictionary<string, TinyUser> m_UserMgr = new Dictionary<string, TinyUser>();

        public void setRuntime(ClientLib.TinyClientRuntime runtime)
        {
            m_runtime = runtime;
            TCPclient = m_runtime.g_TCPclient;
        }

        private void ProcessReceivedMsg()
        {
            switch (TCPclient.getMsgIndex())
            {
                case GlobalDefine.CLIENT_ACTION:
                    {
                        switch (TCPclient.getMsgItem())
                        {
                            case GlobalDefine.CLIENT_SEND_TXTMSG:
                                {
                                    string username = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_USERNAME);
                                    try
                                    {
                                        TinyUser user = m_UserMgr[username];
                                        user.ProcessReceivedMsg();
                                    }
                                    catch (KeyNotFoundException)
                                    {
                                        TCPclient.setMsgIndex(GlobalDefine.CLIENT_ACTION);
                                        TCPclient.setMsgItem(GlobalDefine.CLIENT_SEND_TXTMSG_ERROR);
                                        TCPclient.send();
                                    }
                                } break;
                            case GlobalDefine.CLIENT_SEND_TXTMSG_FINISHED:
                                {
                                    string username = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENT_SEND_TXTMSG_TARGET);
                                    try
                                    {
                                        TinyUser target = m_UserMgr[username];
                                        target.ProcessReceivedMsg();
                                    }
                                    catch (KeyNotFoundException)
                                    {
                                    }
                                } break;
                            default:
                                break;
                        }
                    } break;
                default:
                    break;
            }
            return;
        }

        public MainForm()
        {
            InitializeComponent();

            timerMsg.Enabled = true;
            timerMsg.Start();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

            /*if (TCPclient.connect(m_runtime.g_Config.m_serverIP, m_runtime.g_Config.m_serverPort) == false)
            {
                MessageBox.Show("服务器连接失败");
                return;
            }*/

            TCPclient.setMsgIndex(GlobalDefine.CLIENT_ACTION);
            TCPclient.setMsgItem(GlobalDefine.CLIENT_FRIENDLIST_REQUEST);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTFRIENDLIST_MODULENAME, m_runtime.g_Config.m_ModuleName);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTFRIENDLIST_MODULETYPE, m_runtime.g_Config.m_ModuleType);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTFRIENDLIST_USERNAME, m_runtime.g_Config.m_userName);
            TCPclient.setDefaultParamByIndex(GlobalDefine.CLIENTFRIENDLIST_REGION, m_runtime.g_Config.m_region);


            //file param
            if (TCPclient.talk() == true &&
                TCPclient.getMsgIndex() == GlobalDefine.CLIENT_ACTION && TCPclient.getMsgItem() == GlobalDefine.CLIENT_FRIENDLIST_SUCC)
            {
                //fill UI
                this.UserName.Text = m_runtime.g_Config.m_userName;
                //FriendtreeView.Nodes[0];
                TreeNode tRoot = new TreeNode("My Friends", 0, 0);
                FriendtreeView.Nodes.Add(tRoot);
                string friends = TCPclient.getDefaultParamByIndex(GlobalDefine.CLIENTFRIENDLISTSUCC_FRIENDLIST);
                string[] users = friends.Split(',');
                foreach (string test in users)
                {
                    if (test == "")
                        break;
                    string username = test.Substring(0, test.IndexOf(':'));
                    TreeNode user = new TreeNode(username);
                    TinyUser tempUser = new TinyUser(username);
                    tempUser.setRunTime(m_runtime);
                    m_UserMgr.Add(username, tempUser);
                    tRoot.Nodes.Add(user);
                }
                tRoot.Expand();
            }
            else
            {
                //logon failed!
            }

        }

        private void timerMsg_Tick(object sender, EventArgs e)
        {
            while (TCPclient.nextMsg())
            {
                //msg process
                ProcessReceivedMsg();
            }
        }

        private void CallToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void ShowInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UserInfo ui = null;
            try
            {
                ui = m_UserInfoFormMgr[m_SelectedNode.Text];
                if (ui.IsDisposed)
                {
                    ui = null;
                    m_UserInfoFormMgr.Remove(m_SelectedNode.Text);
                }
            }
            catch (KeyNotFoundException)
            {
            }

            if (ui == null)
            {
                ui = new UserInfo();
                ui.Text = m_SelectedNode.Text;
                m_UserInfoFormMgr.Add(m_SelectedNode.Text, ui);
                ui.Show();
            }

            ui.Activate();
        }

        private void FriendtreeView_MouseDown(object sender, MouseEventArgs e)
        {
            m_SelectedNode = FriendtreeView.GetNodeAt(e.X, e.Y);
        }

        private void FriendtreeView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            try
            {
                TinyUser user = m_UserMgr[m_SelectedNode.Text];
                if(user!= null)
                {
                    user.showTextTalkForm();
                }
            }
            catch (KeyNotFoundException)
            {
               // TinyUser user = new TinyUser();

            }
        }

        private void textToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                TinyUser user = m_UserMgr[m_SelectedNode.Text];
                if (user != null)
                {
                    user.showTextTalkForm();
                }
            }
            catch (KeyNotFoundException)
            {
                // TinyUser user = new TinyUser();

            }
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            //implement magnetism  window here
            //try to iterator all form in form dictionary and action here....
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

    }
}